# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
from scrapy.pipelines.images import ImagesPipeline
from scrapy import Request
import pymysql
class DangdangPipeline(object):
    def process_item(self, item, spider):
        return item

class MysqlPipeline(object):
    def __init__(self,host,user,password,database,port):
        self.host=host
        self.user=user
        self.password=password
        self.database=database
        self.port=port

    @classmethod
    def from_crawler(cls,crawler):
        return cls(
            host = crawler.settings.get("MYSQL_HOST"),
            password = crawler.settings.get("MYSQL_PASS"),
            user = crawler.settings.get("MYSQL_USER"),
            database = crawler.settings.get("MYSQL_DATABASE"),
            port = crawler.settings.get("MYSQL_PORT")

        )
    def open_spider(self,spider):
        self.db = pymysql.connect(self.host,self.user,self.password,self.database,charset='utf8',port=self.port)
        self.cursor=self.db.cursor()

    def process_item(self, item, spider):
        sql='insert into dd(title,img,author,name,publish,price,detail,time) values("%s","%s","%s","%s","%s","%s","%s","%s")'%(item['title'],item['img'],item['author'],item['name'],item['publish'],item['price'],item['detail'],item['time'])
        self.cursor.execute(sql)
        self.db.commit()
        return item
    def close_spider(self,spider):
        self.db.close()

class ImagePipeline(ImagesPipeline):
    def get_media_requests(self, item, info):
        yield Request(item['img'])